<template>
  <div class="eryu-up-item">
    <div
      class="eryu-up-img"
      :class="!read ? 'zindex-m' : ''"
      v-if="item.isImage"
      :style="{
        'background-image': 'url(' + (item.fileUrl || item.raw ) + ')',
      }"
    >
      <div class="zindex-m-show" v-if="!read">
        <i class="el-icon-delete" @click="handleDelete(index)"></i>
      </div>
    </div>

    <div v-else class="eryu-up-file zindex-m" :title="item.name">
      <div class="file-icon">
        <i class="el-icon-document"></i>
      </div>
      <div class="file-name">{{ item.name || item.fileName }}</div>
      <div class="zindex-m-show">
        <i class="el-icon-delete" v-if="!read" @click="handleDelete(index)"></i>
        <i class="el-icon-view" @click="openUrl(item)"></i>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "EryuUpFiles",
  props: {
    // 可以上传的文件类型
    item: {
      type: Object,
      default: () => {
        return {};
      },
    },

    index: {
      type: Number,
      default: 0,
    },

    // 是否为只读状态 默认不为读
    read: {
      type: Boolean,
      default: false,
    },
  },

  watch: {},
  computed: {},
  data() {
    return {};
  },

  created() {},

  methods: {
    handleDelete() {
      this.$emit("onDelete", this, this.index);
    },

    openUrl(item) {
      window.open(item.fileUrl, "_blank");
    },
  },
};
</script>
<style lang="scss" scoped>
.eryu-up-item {
  padding-left: 20px;

  .eryu-up-img {
    cursor: pointer;
    width: 96px;
    height: 102px;
    background-repeat: no-repeat;
    background-position: 50% 50%;
    background-size: cover;
    background-color: #fbfbfb;
    border-radius: 4px;
    border: 1px solid #d9d9d9;
  }

  .eryu-up-file {
    width: 96px;
    height: 102px;
    background: #fbfbfb;
    border-radius: 4px;
    border: 1px solid #d9d9d9;
    font-size: 14px;
    text-align: center;
    display: block;
    cursor: pointer;

    .file-icon {
      text-align: center;
      width: 100%;
      font-size: 32px;
      display: block;
      padding: 12px 0px;
    }
    .file-name {
      text-align: center;
      padding: 4px 4px;
      font-size: 12px;
      white-space: nowrap;
      line-height: 1em;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }

  &.error {
    .eryu-up-img {
      border: 1px solid red;
    }
    .eryu-up-file {
      border: 1px solid red;
      color: red;
    }
  }
}

.zindex-m {
  position: relative;
  .zindex-m-show {
    position: absolute;
    display: none;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
    border-radius: 4px;
    background-color: rgb(0 0 0 / 50%);
    font-size: 14px;
    color: #fff;

    justify-content: center;
    align-items: center;
  }
  &:hover {
    .zindex-m-show {
      display: flex;
    }
  }
}
</style>